
import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:freedom_flutter_mall/utils/router_utils.dart';
import 'package:freedom_flutter_mall/utils/theme_utils.dart';
import 'package:freedom_flutter_mall/values/color_values.dart';
import 'package:freedom_flutter_mall/views/screens/home/home_screen.dart';
import 'package:freedom_flutter_mall/views/widgets/load_image.dart';

class SearchScreenBar extends StatefulWidget implements PreferredSizeWidget {

  const SearchScreenBar({
    super.key,
    this.hintText = '',
    this.isBack = false,
    this.isCancel = true,
    this.isIcon = false,
    this.onPressed,
  });

  final bool isBack;
  final bool isCancel;
  final bool isIcon;
  final String hintText;
  final void Function(String)? onPressed;

  @override
  State<SearchScreenBar> createState() => _SearchScreenBarState();

  @override
  Size get preferredSize => const Size.fromHeight(48.0);
}

class _SearchScreenBarState extends State<SearchScreenBar> {

  final TextEditingController _controller = TextEditingController();
  final FocusNode _focus = FocusNode();

  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
    _controller.dispose();
    _focus.dispose();
  }

  @override
  Widget build(BuildContext context) {
    final bool isDark = context.isDark;
    final Color iconColor = isDark ? Colours.dark_text_gray : Colours.text_gray_c;

    final Widget backWidget = widget.isBack ? Semantics(
      label: '返回',
      child: SizedBox(
        width: 50.0.w,
        height: 50.0.w,
        child: InkWell(
          onTap: () {
            _focus.unfocus();
            Navigator.maybePop(context);
          },
          borderRadius: BorderRadius.circular(12.0.w),
          child: Padding(
            key: const Key('search_back'),
            padding: EdgeInsets.only(left: 12.0.w, top: 10.w, bottom: 10.w,),
            child: Image.asset(
              'assets/images/ic_back_black.png',
              width: 24.w,
              height: 24.w,
              color: isDark ? Colours.dark_text : Colours.text,
            ),
          ),
        ),
      ),
    ) : Container();

    final Widget textFieldWidget = Expanded(
      child: Container(
        height: 36.0.w,
        decoration: BoxDecoration(
          color: Color.fromRGBO(255, 255, 255, 1),
          borderRadius: BorderRadius.circular(18.0.w),
        ),
        child: TextField(
          controller: _controller,
          focusNode: _focus,
          textInputAction: TextInputAction.search,
          decoration: InputDecoration(
            contentPadding: EdgeInsets.only(left: -8.0.w, right: -16.0.w, bottom: 10.0.w),
            border: InputBorder.none,
            icon: Padding(
              padding: EdgeInsets.only(top: 8.0.w, bottom: 8.0.w, left: 8.0.w),
              child: LoadAssetImage('order/order_search', color: iconColor,),
            ),
            hintText: widget.hintText,
            suffixIcon: GestureDetector(
              child: Semantics(
                label: '清空',
                child: Padding(
                  padding: EdgeInsets.only(left: 16.0.w, top: 8.0.w, bottom: 8.0.w),
                  child: LoadAssetImage('order/order_delete', color: iconColor),
                ),
              ),
              onTap: () {
                SchedulerBinding.instance.addPostFrameCallback((_) {
                  _controller.text = '';
                });
              },
            ),
          ),
          onSubmitted: (String val) {
            _focus.unfocus();
            // 点击软键盘的动作按钮时的回调
            widget.onPressed?.call(val);
          },
        ),
      ),
    );

    final Widget iconWidget = widget.isIcon ? Container(
      child: LoadAssetImage('product/list', width: 40.w, height: 40.w,),
    ) : Container();

    final Widget searchWidget = widget.isCancel ? InkWell(
      onTap:() {
        RouterUtils.toPage(context, const HomeScreen());
      },
      child: Padding(
        padding: EdgeInsets.symmetric(vertical: 12.w,),
        child: Text(
          '取消',
          style: TextStyle(
            fontSize: 14.w,
            fontWeight: FontWeight.w400,
            color: const Color.fromARGB(102, 102, 102, 1),
          ),
        ),
      ),
    ) : Container();

    return AnnotatedRegion<SystemUiOverlayStyle>(
      value: ThemeUtils.dark,
      child: Material(
        color: context.backgroundColor,
        child: SafeArea(
          child: Row(
            children: [
              backWidget,
              SizedBox(width: 10.w,),
              textFieldWidget,
              SizedBox(width: 10.w,),
              searchWidget,
              SizedBox(width: 8.w,),
              iconWidget,
            ],
          )
        ),
      ),
    );
  }

}

